package com.xu.cloud.common.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.TimeZone;

/**
 * 日期工具类
 *
 * @author xuguan
 * @since 2024/1/7
 */
@SuppressWarnings("SpellCheckingInspection")
public class LocalDateTimeUtil {
    public static final String NORM_DATE_PATTERN = "yyyy-MM-dd";
    public static final String NORM_TIME_PATTERN = "HH:mm:ss";
    public static final String NORM_TIME_MS_PATTERN = "HH:mm:ss.SSS";
    public static final String NORM_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String NORM_DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String PURE_DATE_PATTERN = "yyyyMMdd";
    public static final String PURE_TIME_PATTERN = "HHmmss";
    public static final String PURE_TIME_MS_PATTERN = "HHmmssSSS";
    public static final String PURE_DATE_TIME_PATTERN = "yyyyMMddHHmmss";
    public static final String PURE_DATE_TIME_MS_PATTERN = "yyyyMMddHHmmssSSS";

    public static final ZoneId ZONE_ID_EAST_8 = ZoneId.of("+8");
    public static final TimeZone TIME_ZONE_EAST_8 = TimeZone.getTimeZone(ZoneId.of("+8"));

    public static String fmtCurrentNormDate() {
        return fmtCurrent(NORM_DATE_PATTERN);
    }

    public static String fmtCurrentPureDate() {
        return fmtCurrent(PURE_DATE_PATTERN);
    }

    public static String fmtCurrentNormTime() {
        return fmtCurrent(NORM_TIME_PATTERN);
    }

    public static String fmtCurrentPureTime() {
        return fmtCurrent(PURE_TIME_PATTERN);
    }

    public static String fmtCurrentNormDateTime() {
        return fmtCurrent(NORM_DATE_TIME_PATTERN);
    }

    public static String fmtCurrentPureDateTime() {
        return fmtCurrent(PURE_DATE_TIME_PATTERN);
    }

    public static String fmtNormDate(LocalDate date) {
        return format(date, NORM_DATE_PATTERN);
    }

    public static String fmtPureDate(LocalDate date) {
        return format(date, PURE_DATE_PATTERN);
    }

    public static String fmtNormTime(LocalTime time) {
        return format(time, NORM_TIME_PATTERN);
    }

    public static String fmtPureTime(LocalTime time) {
        return format(time, PURE_TIME_PATTERN);
    }

    public static String fmtNormDatetime(LocalDateTime dateTime) {
        return format(dateTime, NORM_DATE_TIME_PATTERN);
    }

    public static String fmtPureDatetime(LocalDateTime dateTime) {
        return format(dateTime, PURE_DATE_TIME_PATTERN);
    }

    public static LocalDate parseNormDate(String date) {
        return parseDate(date, NORM_DATE_PATTERN);
    }

    public static LocalDate parsePureDate(String date) {
        return parseDate(date, PURE_DATE_PATTERN);
    }

    public static LocalTime parseNormTime(String time) {
        return parseTime(time, NORM_TIME_PATTERN);
    }

    public static LocalTime parsePureTime(String time) {
        return parseTime(time, PURE_TIME_PATTERN);
    }

    public static LocalDateTime parseNormDatetime(String dateTime) {
        return parseDateTime(dateTime, NORM_DATE_TIME_PATTERN);
    }

    public static LocalDateTime parsePureDatetime(String dateTime) {
        return parseDateTime(dateTime, PURE_DATE_TIME_PATTERN);
    }

    public static String fmtCurrent(String pattern) {
        return format(currentDateTime(), pattern);
    }

    public static String format(TemporalAccessor temporalAccessor, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(temporalAccessor);
    }

    public static LocalDate parseDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalTime parseTime(String time, String pattern) {
        return LocalTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDateTime parseDateTime(String dateTime, String pattern) {
        return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate currentDate() {
        return LocalDate.now(ZONE_ID_EAST_8);
    }

    public static LocalTime currentTime() {
        return LocalTime.now(ZONE_ID_EAST_8);
    }

    public static LocalDateTime currentDateTime() {
        return LocalDateTime.now(ZONE_ID_EAST_8);
    }
}
